package com.tiancheng.trade.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.exception.error.BusinessErrorInfoEnum;
import com.tiancheng.trade.merchant.mapper.BusinessMapper;
import com.tiancheng.trade.merchant.mapper.OrgBusinessMapper;
import com.tiancheng.trade.merchant.model.Business;
import com.tiancheng.trade.merchant.model.OrgBusiness;
import com.tiancheng.trade.merchant.service.IBusinessService;
import com.tiancheng.trade.merchant.util.IdGenerateUtil;
import com.tiancheng.trade.merchant.vo.merchant.QueryPageBusinessVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: kellen
 * @Description: 业务管理 service impl
 * @Date: create in 2024/11/19 14:02
 */
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements IBusinessService {
    @Resource
    private OrgBusinessMapper orgBusinessMapper;
    @Resource
    private IdGenerateUtil idGenerateUtil;
    @Override
    public String newBusiness(Business business) {
        final long count = this.count(new LambdaQueryWrapper<Business>().eq(Business::getBusinessName, business.getBusinessName()));
        BusinessErrorInfoEnum.SYSTEM_ERROR.assertIsTrue(count==0,"业务名称重复！");
        business.setCreatedDt(LocalDateTime.now());
        business.setBusinessCode(idGenerateUtil.getBusinessCode(business.getBusinessName()));
        this.save(business);
        return business.getBusinessCode();
    }

    @Override
    public PageInfo<Business> queryPage(QueryPageBusinessVO businessVO) {
        return this.baseMapper.find(new LambdaQueryWrapper<Business>().eq(StringUtils.isNotEmpty(businessVO.getBusinessCode()), Business::getBusinessCode, businessVO.getBusinessCode())
                .like(StringUtils.isNotEmpty(businessVO.getBusinessCode()), Business::getBusinessCode, businessVO.getBusinessCode()), businessVO.getCurrentPage(), businessVO.getPageSize());
    }

    @Override
    public List<Business> queryList(QueryPageBusinessVO businessVO) {
        return this.list(new LambdaQueryWrapper<Business>().eq(StringUtils.isNotEmpty(businessVO.getBusinessCode()), Business::getBusinessCode, businessVO.getBusinessCode())
                .like(StringUtils.isNotEmpty(businessVO.getBusinessCode()), Business::getBusinessCode, businessVO.getBusinessCode()));
    }

    @Override
    public void saveBusiness(Business business) {
        if(null!=business.getId()){
            final long count = this.count(new LambdaQueryWrapper<Business>().eq(Business::getId, business.getId()).or().eq(Business::getBusinessName, business.getBusinessName()));
            BusinessErrorInfoEnum.SYSTEM_ERROR.assertIsTrue(count<2,"业务名称不允许重复！");
            this.updateById(business);
        }else{
            newBusiness(business);
        }
    }

    @Override
    public void delete(Long id) {
        final Business business = this.getById(id);
        final long count = orgBusinessMapper.selectCount(new LambdaQueryWrapper<OrgBusiness>().eq(OrgBusiness::getBusinessCode, business.getBusinessCode()));
        BusinessErrorInfoEnum.SYSTEM_ERROR.assertIsTrue(count==0,"已有商户开通该业务不允许删除！");
        this.removeById(id);
    }
}
